//
//  audioViewController.m
//  fugitiveVertical
//
//  Created by Eugene Bistolas on 5/12/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "audioViewController.h"
#import <AVFoundation/AVFoundation.h>

@implementation audioViewController

@synthesize recButton;
@synthesize playButton;
@synthesize stopButton;

- (IBAction) recording {
    
    //if was not recording, record....
    if (!isRecording) {
        
        /*
        //set recording settings
        NSMutableDictionary *recordSettings = [[NSMutableDictionary alloc] initWithCapacity:10];
        NSNumber *formatObject = [NSNumber numberWithInt: kAudioFormatMPEG4AAC];
        [recordSettings setObject:formatObject forKey: AVFormatIDKey];
        [recordSettings setObject:[NSNumber numberWithFloat:44100.0] forKey: AVSampleRateKey];
        [recordSettings setObject:[NSNumber numberWithInt:2] forKey:AVNumberOfChannelsKey];
        [recordSettings setObject:[NSNumber numberWithInt:12800] forKey:AVEncoderBitRateKey];
        [recordSettings setObject:[NSNumber numberWithInt:16] forKey:AVLinearPCMBitDepthKey];
        [recordSettings setObject:[NSNumber numberWithInt: AVAudioQualityHigh] forKey: AVEncoderAudioQualityKey];
         */
        
        //toggle isRecording
        isRecording = YES;
        
        //adjust button
        [recButton setTitle:@"STOP" forState:UIControlStateNormal];
        playButton.hidden = YES;
        recordingLabel.text = @"Recording";
        
        tempRecFile = [NSURL fileURLWithPath:[NSTemporaryDirectory() stringByAppendingPathComponent:[NSString stringWithString:@"VoiceFile"]]];
        if (recorder) {
            [recorder release]; 
        }
        recorder = [[AVAudioRecorder alloc] initWithURL:tempRecFile settings:nil error:nil];
        [recorder setDelegate:self];
        [recorder prepareToRecord];
        [recorder record];
    
    //else if it was recording, stop recording
    } else {
        isRecording = NO;
        [recButton setTitle:@"REC" forState:UIControlStateNormal];
        playButton.hidden = NO;
        recordingLabel.text = @"Not Recording";
        [recorder stop];
        
    }
}
- (IBAction) playback {
    
    stopButton.hidden = NO;
    
    UInt32 sessionCategory = kAudioSessionCategory_MediaPlayback;
    AudioSessionSetProperty(kAudioSessionProperty_AudioCategory, sizeof(sessionCategory), &sessionCategory);    
    UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_Speaker;
    AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
    
    AVAudioPlayer *player = [[AVAudioPlayer alloc] initWithContentsOfURL:tempRecFile error:nil];
    player.volume = 1;
    [player play];
    
    [player release]; 

    //if stop is pressed, 
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    [super dealloc];
    [playButton release];
    [recButton release];
    [recorder release]; 

}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    //set up UI buttons
    self.title = @"APIs";
    UIImage *normalImg = [[UIImage imageNamed:@"regular.png"] stretchableImageWithLeftCapWidth:12 topCapHeight:0];
    UIImage *highlightImg = [[UIImage imageNamed:@"hover.png"] stretchableImageWithLeftCapWidth:12 topCapHeight:0];
    UIImage *activeImg = [[UIImage imageNamed:@"active.png"] stretchableImageWithLeftCapWidth:12 topCapHeight:0];
    
    [recButton setBackgroundImage:normalImg forState:UIControlStateNormal]; 
    [recButton setBackgroundImage:highlightImg forState:UIControlStateHighlighted];
    [recButton setBackgroundImage:activeImg forState:UIControlStateSelected];
    [playButton setBackgroundImage:normalImg forState:UIControlStateNormal]; 
    [playButton setBackgroundImage:highlightImg forState:UIControlStateHighlighted];
    [playButton setBackgroundImage:activeImg forState:UIControlStateSelected];

    [[self navigationController] setNavigationBarHidden:YES animated:NO];  
    
    
    
    isRecording = NO;
    playButton.hidden = YES;
    stopButton.hidden = YES;
    recordingLabel.text = @"Not Recording";

    AVAudioSession  *audioSession = [AVAudioSession sharedInstance];
    [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
    [audioSession setActive:YES error:nil];
    

}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    NSFileManager *fileHandler = [NSFileManager defaultManager];
    [fileHandler removeItemAtPath:[tempRecFile absoluteString] error:nil];
    [fileHandler release];
    recorder = nil;
    tempRecFile = nil;
    playButton.hidden = YES;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
